package com.sec.etech.project.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sec.etech.project.mapper.ProjOrdersComponentTwiceworksMapper;
import com.sec.etech.project.model.ProjOrdersComponentTwiceworks;
import com.sec.etech.project.model.ProjectOrdersComponent;
import org.apache.commons.collections4.MapUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WorkChangeService {

    @Resource
    JdbcTemplate jdbcTemplate;
    @Resource
    ProjectOrdersComponentService ordersComponentService;
    @Resource
    ProjOrdersComponentTwiceworksMapper ordersComponentTwiceworksMapper;

    @Cacheable("getTwiceworkname")
    public String getTwiceworkname(Long twiceworkid){
        String dictsql = "select twiceworkid,twiceworkname,onceworkid from proj_order_z_twicework where active = 1";
        List<Map<String, Object>> dictList = jdbcTemplate.queryForList(dictsql);
        Map<Long, Map> dictMap = new HashMap<>();
        for (Map map : dictList) {
            dictMap.put(MapUtil.getLong(map, "twiceworkid"), map);
        }
        Map map = dictMap.get(twiceworkid);
        String twiceworkname = MapUtils.getString(map, "twiceworkname", "");

        if (StrUtil.isNotBlank(twiceworkname)) {
            twiceworkname = "采购(" + twiceworkname + ")";
        }
        return twiceworkname;
    }

    /*public String getProdWorkname(Long onceworkid, Long twiceworkid, Long orderscomponentid){
        String twiceworkname = getTwiceworkname(twiceworkid);
        //采购
        if (onceworkid.intValue() == 1 || onceworkid.intValue() == 4 || onceworkid.intValue() == 13 || onceworkid.intValue() == 15) {
            return twiceworkname;
        } else {
            //采购加制造
            //查询制造的
            String twiceworkname2 = getProdworknameByOrderscomponentid(orderscomponentid, 2);
            twiceworkname = twiceworkname + (StrUtil.isNotBlank(twiceworkname) && StrUtil.isNotBlank(twiceworkname2) ? "、" : "") + twiceworkname2;
            return twiceworkname;
        }
    }*/

    /**
     * 获取新的完工车间
     *
     * @param orderscomponentid 清单明细id
     * @param type              1:采购 2制造
     */
    public String getProdworknameByOrderscomponentid(Long orderscomponentid, Integer type) {
        String sql = "select GROUP_CONCAT(twiceworkname order by field(twiceworkid,{}) SEPARATOR '、') from proj_order_z_twicework where active = 1 and twiceworkid in ({})";
        ProjectOrdersComponent projectOrdersComponent = ordersComponentService.getById(orderscomponentid);
        if (type == 1) {
            if (projectOrdersComponent.getTwiceworkid() != null) {
                String idsStr = StrUtil.format(sql, projectOrdersComponent.getTwiceworkid(), projectOrdersComponent.getTwiceworkid());
                return jdbcTemplate.queryForObject(idsStr, String.class);
            }
        } else {
            List<ProjOrdersComponentTwiceworks> list = ordersComponentTwiceworksMapper.selectList(Wrappers.<ProjOrdersComponentTwiceworks>lambdaQuery()
                    .eq(ProjOrdersComponentTwiceworks::getOrderscomponentid, orderscomponentid)
                    .eq(ProjOrdersComponentTwiceworks::getActive, 1)
                    .orderByAsc(ProjOrdersComponentTwiceworks::getOrderby)
            );
            List<Long> ids = new ArrayList<>();
            for (ProjOrdersComponentTwiceworks twiceworks : list) {
                ids.add(twiceworks.getTwiceworkid());
            }
            if (CollUtil.isNotEmpty(ids)) {
                String idsStr = StrUtil.format(sql, CollUtil.join(ids, ","), CollUtil.join(ids, ","));
                String twiceworknameStr = jdbcTemplate.queryForObject(idsStr, String.class);
                // 完工车间仅装配 选是的话最后一个要加（）
                if (projectOrdersComponent.getIsOnlyAssemble() != null && projectOrdersComponent.getIsOnlyAssemble() == 1) {
                    if (StrUtil.isNotBlank(twiceworknameStr)) {
                        String[] ss = twiceworknameStr.split("、");
                        ss[ss.length - 1] = "(" + ss[ss.length - 1] + ")";
                        twiceworknameStr = CollUtil.join(CollUtil.toList(ss), "、");
                    }
                }
                return twiceworknameStr;
            }
        }
        return "";
    }
}
